<?php

namespace app\api\controller\kuyou\applet;

use app\api\library\kuyou\Auths as Auth;
use think\Config;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Response;
use think\Route;
use think\Validate;

/**
 * API控制器基类
 */
class Common
{

    /**
     * @var Request Request 实例
     */
    protected $request;

    /**
     * @var bool 验证失败是否抛出异常
     */
    protected $failException = false;

    /**
     * @var bool 是否批量验证
     */
    protected $batchValidate = false;

    /**
     * @var array 前置操作方法列表
     */
    protected $beforeActionList = [];

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 权限Auth
     * @var Auth
     */
    protected $auth = null;

    /**
     * 默认响应输出类型,支持json/xml
     * @var string
     */
    protected $responseType = 'json';

    /**
     * 构造方法
     * @access public
     * @param Request $request Request 对象
     */
    public function __construct(Request $request = null)
    {
        $this->request = is_null($request) ? Request::instance() : $request;

        // 控制器初始化
        $this->_initialize();

        // 前置操作方法
        if ($this->beforeActionList) {
            foreach ($this->beforeActionList as $method => $options) {
                is_numeric($method) ?
                    $this->beforeAction($options) :
                    $this->beforeAction($method, $options);
            }
        }
    }

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {
        //跨域请求检测
        check_cors_request();

        // 检测IP是否允许
        check_ip_allowed();

        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');

        $this->auth = Auth::instance();

        $modulename = $this->request->module();
        $controllername = Loader::parseName($this->request->controller());
        $actionname = strtolower($this->request->action());

        // token
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        // 检测是否需要验证登录
        if (!$this->auth->match($this->noNeedLogin)) {
            //检测是否登录
            if (!$this->auth->isLogin($token)) {
                $this->error(__('Please login first'), null, 401);
            }
            // 判断是否需要验证权限
            if (!$this->auth->match($this->noNeedRight)) {
                // 判断控制器和方法判断是否有对应权限
                $path = str_replace('applet/', '', $path);
                if (!$this->auth->check($path,$token)) {
                    $this->error(__('You have no permission'), null, 403);
                }
            }
        } else {
            // 如果有传递token才验证是否登录状态
            if ($token) {
                if (!$this->auth->isLogin($token)) {
                    $this->error(__('Please login first'), null, 401);
                }
            }
        }

        $upload = \app\common\model\Config::upload();

        // 上传信息配置后
        Hook::listen("upload_config_init", $upload);

        Config::set('upload', array_merge(Config::get('upload'), $upload));

        // 加载当前控制器语言包
        $this->loadlang($controllername);
    }

    /**
     * 加载语言文件
     * @param string $name
     */
    protected function loadlang($name)
    {
        $name = Loader::parseName($name);
        Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
    }

    /**
     * 操作成功返回的数据
     * @param string $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为1
     * @param string $type   输出类型
     * @param array  $header 发送的 Header 信息
     */
    protected function success($msg = '', $data = null, $code = 200, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }

    /**
     * 操作失败返回的数据
     * @param string $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型
     * @param array  $header 发送的 Header 信息
     */
    protected function error($msg = '', $data = null, $code = 500, $type = null, array $header = [])
    {
        $this->result($msg, $data, $code, $type, $header);
    }

    function format_field($data,$type='json'){
        if(is_object($data) || is_array($data)){
            if(is_object($data)){
                if($type!='json'){
                    return $data;
                }
                $data = json_encode($data, JSON_UNESCAPED_UNICODE);
                $data = json_decode($data,true);
            }
            $price_decimal = $this->getSet('price_decimal','value');$number_decimal = $this->getSet('number_decimal','value');
            $price_arr = [];/*需格式化价格字段*/
            $number_arr = [];/*需格式化数量字段*/
            foreach ($data as $k=>$v){
                if(strpos($k,'image') !== false && !empty($v)){
                    if(!is_array($v) && strpos($k,'images') !== false){
                        $v = explode(',',$v);
                    }
                    if(is_array($v)){
                        foreach ($v as $v_k=>$v_v){
                            $v[$v_k] = $v_v ? cdnurl($v_v, true) : '';
                        }
                    }else{
                        $v = $v ? cdnurl($v, true) : '';
                    }
                    $data[$k] = $v;
                }else{
                    if(!is_array($v)){
                        if(in_array($k,$price_arr) && !empty($k)){
                            $data[$k] = $this->format_number($v,$price_decimal);
                        }else if(in_array($k,$number_arr) && !empty($k)){
                            $data[$k] = $this->format_number($v,$number_decimal);
                        }else if($v===null){
                            $data[$k] = '';
                        }
                    }else{
                        $data[$k] = $this->format_field($v,$type);
                    }
                }
            }
        }
        return $data;
    }

    function format_number($data,$number){
        $data = round($data,$number);
        if($data!=='' && $number>0){
            $arr = explode('.',$data);
            if(isset($arr[1]) && sizeof($arr)==2){
                if(strlen($arr[1])<$number){
                    $str = (string)$arr[1];
                    for ($a=1;$a<=($number-strlen($arr[1]));$a++){
                        $str .= '0';
                    }
                    $data = $arr[0].'.'.$str;
                }
            }else if(isset($arr[0]) && sizeof($arr)==1){
                $str = '';
                for ($a=1;$a<=$number;$a++){
                    $str .= '0';
                }
                $data = $arr[0].'.'.$str;
            }
        }
        return (string)$data;
    }

    function getSet($name,$field)
    {
        $set = \think\Db::name('kuyou_sets')->where(['name'=>$name])->value($field);
        return $set;
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @access protected
     * @param mixed  $msg    提示信息
     * @param mixed  $data   要返回的数据
     * @param int    $code   错误码，默认为0
     * @param string $type   输出类型，支持json/xml/jsonp
     * @param array  $header 发送的 Header 信息
     * @return void
     * @throws HttpResponseException
     */
    protected function result($msg, $data = null, $code = 500, $type = null, array $header = [])
    {
        $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
        $data = $this->format_field($data,$type);
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }

    /**
     * 前置操作
     * @access protected
     * @param string $method  前置操作方法名
     * @param array  $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
     * @return void
     */
    protected function beforeAction($method, $options = [])
    {
        if (isset($options['only'])) {
            if (is_string($options['only'])) {
                $options['only'] = explode(',', $options['only']);
            }

            if (!in_array($this->request->action(), $options['only'])) {
                return;
            }
        } elseif (isset($options['except'])) {
            if (is_string($options['except'])) {
                $options['except'] = explode(',', $options['except']);
            }

            if (in_array($this->request->action(), $options['except'])) {
                return;
            }
        }

        call_user_func([$this, $method]);
    }

    /**
     * 设置验证失败后是否抛出异常
     * @access protected
     * @param bool $fail 是否抛出异常
     * @return $this
     */
    protected function validateFailException($fail = true)
    {
        $this->failException = $fail;

        return $this;
    }

    /**
     * 验证数据
     * @access protected
     * @param array        $data     数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array        $message  提示信息
     * @param bool         $batch    是否批量验证
     * @param mixed        $callback 回调方法（闭包）
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
    {
        if (is_array($validate)) {
            $v = Loader::validate();
            $v->rule($validate);
        } else {
            // 支持场景
            if (strpos($validate, '.')) {
                list($validate, $scene) = explode('.', $validate);
            }

            $v = Loader::validate($validate);

            !empty($scene) && $v->scene($scene);
        }

        // 批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }
        // 设置错误信息
        if (is_array($message)) {
            $v->message($message);
        }
        // 使用回调验证
        if ($callback && is_callable($callback)) {
            call_user_func_array($callback, [$v, &$data]);
        }

        if (!$v->check($data)) {
            if ($this->failException) {
                throw new ValidateException($v->getError());
            }

            return $v->getError();
        }

        return true;
    }

    /**
     * 刷新Token
     */
    protected function token()
    {
        $token = $this->request->param('__token__');

        //验证Token
        if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
            $this->error(__('Token verification error'), ['__token__' => $this->request->token()]);
        }

        //刷新Token
        $this->request->token();
    }

    /**
     * 获取编号
     * @param int $type   编号类型
     * @return mixed
     */
    function getCodes($code,$type)
    {
        $set = \think\Db::name(config('app_prefix').'kuyou_prefix')->where(['type'=>$type])->find();
        if(empty($set)){
            return '';
        }else{
            preg_match_all("/\d+/",$code,$num);
            $order_num = implode('',$num[0]);
            if($order_num<$set['start']){
                $order_num = $set['start'];
            }else{
                $order_num = (float)$order_num+1;
            }
            $prefix = $set['prefix'];
            if($set['year']==1){
                $prefix = $prefix.date('Y');
            }
            if($set['month']==1){
                $prefix = $prefix.date('m');
            }
            if($set['day']==1){
                $prefix = $prefix.date('d');
            }
            if($set['hour']==1){
                $prefix = $prefix.date('H');
            }
            if($set['minute']==1){
                $prefix = $prefix.date('i');
            }
            if($set['second']==1){
                $prefix = $prefix.date('s');
            }
            $s = '';
            if(strlen($order_num)<$set['strlen']){
                for ($a=1;$a<=$set['strlen']-(strlen($order_num));$a++){
                    $s .= '0';
                }
            }
            $code = $order_num;
            $order_num = $prefix.$s.$order_num;
        }
        if(!empty($set['dbname']) && !empty($set['field'])){
            $res = \think\Db::name($set['dbname'])->where([$set['field']=>$order_num])->value('id');
            if($res){
                return $this->getCodes($code,$type);
            }
        }
        return $order_num;
    }
}
